package com.zsh.wanandroid.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.zsh.wanandroid.common.APP;
import com.zsh.wanandroid.impl.DialogClickListener;

import static android.content.Context.INPUT_METHOD_SERVICE;

/**
 * @author zsh
 * @date 2019/10/21.
 * 功能：专门提供为处理一些UI相关的问题而创建的工具类，提供资源获取的通用方法，避免每次都写重复的代码获取结果。
 */
public class UIUtils {

    public static Context getContext(){
        return APP.mContext;
    }

    public static Handler getHandler(){
        return APP.mHandler;
    }

    //返回指定colorId对应的颜色值
    public static int getColor(int colorId){
        return getContext().getResources().getColor(colorId);
    }

    //加载指定viewId的视图对象，并返回
    public static View getView(int viewId){
        View view = View.inflate(getContext(), viewId, null);
        return view;
    }

    public static String[] getStringArr(int strArrId){
        String[] stringArray = getContext().getResources().getStringArray(strArrId);
        return stringArray;
    }

    //将dp转化为px
    public static int dp2px(int dp){
        //获取手机密度
        float density = getContext().getResources().getDisplayMetrics().density;
        return (int) (dp * density + 0.5);//实现四舍五入
    }

    public static int px2dp(int px){
        //获取手机密度
        float density = getContext().getResources().getDisplayMetrics().density;
        return (int) (px / density + 0.5);//实现四舍五入
    }

    //保证runnable中的操作在主线程中执行
    public static void runOnUiThread(Runnable runnable) {
        if(isInMainThread()){
            runnable.run();
        }else{
            UIUtils.getHandler().post(runnable);
        }
    }
    //判断当前线程是否是主线程
    private static boolean isInMainThread() {
        int currentThreadId = android.os.Process.myTid();
        return APP.mainThreadId == currentThreadId;

    }

    /**
     * 隐藏键盘
     */
    public static void hideInput(Activity activity) {
        InputMethodManager imm = (InputMethodManager) getContext().getSystemService(INPUT_METHOD_SERVICE);
        View v = activity.getWindow().peekDecorView();
        if (null != v) {
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }
    }

    /**
     *禁止软键盘自动弹出
     */
    public static void softInputMode(Activity activity) {
        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    }

    //toast
    public static void showToast(String message){
        Toast.makeText(UIUtils.getContext(), message, Toast.LENGTH_SHORT).show();
    }

    /**
     * dialog提示--有标题，两个按钮，有回调
     * @param activity 当前调用的activity
     * @param themeResId 主题
     * @param title 标题
     * @param msg 内容
     * @param button1 按钮1
     * @param button2 按钮2
     * @param dialogClickListener dialog按钮点击监听接口
     */
    public static void normalDialog(Activity activity, int themeResId, String title, String msg, String button1,
                                    String button2, final DialogClickListener dialogClickListener) {
        //构造对话框
        AlertDialog.Builder builder = new AlertDialog.Builder(activity, themeResId);
        builder.setTitle(title);
        builder.setIcon(android.R.drawable.ic_dialog_alert);
        builder.setMessage(msg);
        //按钮1（取消）
        builder.setNegativeButton(button1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialogClickListener.onNegativeClick();
                dialog.dismiss();
            }
        });
        //按钮2（确定）
        builder.setPositiveButton(button2, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialogClickListener.onPositiveClick();
                dialog.dismiss();
            }
        });
        builder.show();
    }

    /**
     * dialog提示--有标题，两个按钮，有回调
     * @param activity 当前调用的activity
     * @param title 标题
     * @param msg 内容
     * @param button1 按钮1
     * @param button2 按钮2
     * @param dialogClickListener dialog按钮点击监听接口
     */
    public static void normalDialog(Activity activity, String title, String msg, String button1,
                                    String button2, final DialogClickListener dialogClickListener) {
        //构造对话框
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle(title);
        builder.setIcon(android.R.drawable.ic_dialog_alert);
        builder.setMessage(msg);
        //按钮1（取消）
        builder.setNegativeButton(button1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialogClickListener.onNegativeClick();
                dialog.dismiss();
            }
        });
        //按钮2（确定）
        builder.setPositiveButton(button2, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialogClickListener.onPositiveClick();
                dialog.dismiss();
            }
        });
        builder.show();
    }

    /**
     * dialog提示--无标题，两个按钮，有回调
     * @param activity 当前调用的activity
     * @param themeResId 主题
     * @param msg 内容
     * @param button1 按钮1
     * @param button2 按钮2
     * @param dialogClickListener dialog按钮点击监听接口
     */
    public static void normalDialog(Activity activity, int themeResId, String msg, String button1,
                                    String button2, final DialogClickListener dialogClickListener) {
        //构造对话框
        AlertDialog.Builder builder = new AlertDialog.Builder(activity, themeResId);
        builder.setIcon(android.R.drawable.ic_dialog_alert);
        builder.setMessage(msg);
        //按钮1（取消）
        builder.setNegativeButton(button1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialogClickListener.onNegativeClick();
                dialog.dismiss();
            }
        });
        //按钮2（确定）
        builder.setPositiveButton(button2, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialogClickListener.onPositiveClick();
                dialog.dismiss();
            }
        });
        builder.show();
    }

    /**
     * dialog提示--无标题，两个按钮，有回调
     * @param activity 当前调用的activity
     * @param msg 内容
     * @param button1 按钮1
     * @param button2 按钮2
     * @param dialogClickListener dialog按钮点击监听接口
     */
    public static void normalDialog(Activity activity, String msg, String button1,
                                    String button2, final DialogClickListener dialogClickListener) {
        //构造对话框
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setIcon(android.R.drawable.ic_dialog_alert);
        builder.setMessage(msg);
        //按钮1（取消）
        builder.setNegativeButton(button1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialogClickListener.onNegativeClick();
                dialog.dismiss();
            }
        });
        //按钮2（确定）
        builder.setPositiveButton(button2, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialogClickListener.onPositiveClick();
                dialog.dismiss();
            }
        });
        builder.show();
    }

    /**
     * dialog提示--有标题，一个按钮，没有回调
     * @param activity 当前调用的activity
     * @param themeResId 主题
     * @param title 标题
     * @param msg 内容
     * @param button1 按钮1
     */
    public static void normalDialog(Activity activity, int themeResId, String title, String msg, String button1) {
        //构造对话框
        AlertDialog.Builder builder = new AlertDialog.Builder(activity, themeResId);
        builder.setTitle(title);
        builder.setIcon(android.R.drawable.ic_dialog_alert);
        builder.setMessage(msg);
        //按钮1（取消）
        builder.setNegativeButton(button1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.show();
    }

    /**
     * dialog提示--无标题，一个按钮，没有有回调
     * @param activity 当前调用的activity
     * @param themeResId 主题
     * @param msg 内容
     * @param button1 按钮1
     */
    public static void normalDialog(Activity activity, int themeResId, String msg, String button1) {
        //构造对话框
        AlertDialog.Builder builder = new AlertDialog.Builder(activity, themeResId);
        builder.setIcon(android.R.drawable.ic_dialog_alert);
        builder.setMessage(msg);
        //按钮1（取消）
        builder.setNegativeButton(button1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.show();
    }
}
